function eventBuyNum() {
    $('.num-dec').on('click',function () {
        var elem  =$(this).parents('.buy-num').find('input[name="number"]');
        var number = parseInt(elem.val());
        if(number<=1){
            return false;
        }
        number = number-1;
        totalPrice(elem,number);
        countNum();
    });
    $('.num-add').on('click',function () {
        var elem  =$(this).parents('.buy-num').find('input[name="number"]');
        var number = parseInt(elem.val());
        number = number+1;

        totalPrice(elem,number);
        countNum();
    });
    
    function totalPrice(elem,number) {
        var price = parseFloat(elem.data('price'));
        var id = $(elem).data('id');
        $.post($(elem).data('async'),{number:number,'_token':$('meta[name="csrf-token"]').attr('content')},function (response) {
            if(response.status == 1){
                elem.val(number);
                $('#cart-item-'+id+' .item-total').text(price*number);
            }
        });
    }
}

function countNum() {
    var selectedGoods = $('.cart-box input[type="checkbox"]:checked.check-item');
    $('#cart-select-num').text(selectedGoods.length);
    var total = 0;
    selectedGoods.each(function () {
        var price = parseFloat($(this).data('price'));
        var goodsId = parseInt($(this).val());
        var num = $("#cart-item-"+goodsId+' input[name="number"]').val();
        total = total+price*parseInt(num);
    });
    $("#total-money").text(total);
}

function removeGoods() {
    $('.cart-del').each(function () {
        $(this).on('click',function () {
            var cartId = $(this).data('id');
            var url = $(this).data('url');
            layer.confirm('确定要删除该商品吗?',function (index) {
                $.get(url,function (response) {
                    if(response.status == 1){
                        $('#cart-item-'+cartId).remove();
                        countNum();
                    }
                    layer.close(index);
                });
            });

        })
    });

    $('input[type="checkbox"]').each(function () {
        $(this). on('click',function () {
            var parentElem = $(this).parents('.'+$(this).data('box'));

            if ($(this).hasClass('check-all')) {
                parentElem.find("input.check-item,input.check-all").prop("checked", this.checked);
            } else {
                if (!this.checked) {
                    parentElem.find(".check-all").prop("checked", false);
                } else {
                    var preElem = parentElem.find("input.check-item");
                    var checked = true;
                    preElem.each(function () {
                        if (!this.checked) {
                            checked = false;
                        }
                    });
                    parentElem.find("input.check-all").prop("checked", checked);
                }
            }
            countNum();
        });
    });


    $('#cart-clear').on('click',function () {
        var url = $(this).data('url');
        layer.confirm('确定要清空购物车吗?',function (index) {
            $.get(url,function (response) {
                window.location.reload();
            });
        });
    })
}

function createOrder(obj) {
    var formElem = $(obj).parents('form');
    formElem.submit();
}